* Extensions File

set more off

clear

cap n log close

log using 2c1-Extensions.log, replace

*********************************************
** Industry Shares                         **
*********************************************

use ./temp/2000-WorkerResults.dta, clear
gen ct=perwt
gen self=(classwkr==1)
collapse (sum) ct, by(ind1990 self) fast
replace ind1990=ind1990*10+self
egen tl=sum(ct)
gen ctsh=ct/tl
drop tl
gen distr=0 if _n==1
replace distr=distr[_n-1]+ctsh[_n-1] if _n>=2
drop ct ctsh
sort ind1990 self
label values ind1990
save ./temp/2000-Ind-Distribution.dta, replace

use if classwkr==1 using ./temp/2000-WorkerResults.dta, clear
gen ct=1
collapse (sum) ct, by(ind1990) fast
egen tl=sum(ct)
gen ctsh=ct/tl
drop tl
gen distr=0 if _n==1
replace distr=distr[_n-1]+ctsh[_n-1] if _n>=2
drop ct
sort ind1990
label values ind1990
save ./temp/2000-Ind-Distribution-SelfOnly.dta, replace

*********************************
*** ASE Tabulation - Table 9  ***
*********************************

use "D:\Dropbox\Shared\MandorffPaper\data\bk-industry\ASE\SE1600CSCB36.DTA", clear
keep if GEO_TTL=="United States"
keep if ASECB_TTL=="All firms"
drop if NAICS2012_TTL=="Total for all sectors"
keep if year==2016
keep if yibszfi==1
keep PROVADV_TTL EMP_PCT NAICS2012 NAICS2012_TTL
gen advice=""
replace advice="All" if PROVADV_TTL=="All firms"
replace advice="Family" if PROVADV_TTL=="Business sought advice or mentoring from family"
replace advice="Friends" if PROVADV_TTL=="Business sought advice or mentoring from friends"
replace advice="Colleagues" if PROVADV_TTL=="Business sought advice or mentoring from professional colleagues"
replace advice="Employees" if PROVADV_TTL=="Business sought advice or mentoring from employees"
replace advice="Advisors" if PROVADV_TTL=="Business sought advice or mentoring from legal and professional advisors"
replace advice="Customers" if PROVADV_TTL=="Business sought advice or mentoring from customers"
replace advice="Suppliers" if PROVADV_TTL=="Business sought advice or mentoring from suppliers"
replace advice="Government" if PROVADV_TTL=="Business sought advice or mentoring from government-supported technical assistance program"
replace advice="Other" if PROVADV_TTL=="Business sought advice or mentoring from other source"
replace advice="Reporting" if PROVADV_TTL=="Total reporting"
replace advice="Skipped" if PROVADV_TTL=="Item not reported"
replace advice="NA" if PROVADV_TTL=="Not applicable"
for any All Skipped Reporting NA: drop if advice=="X"
drop if NAICS2012==99
gen IMM=0
for any 72 23: replace IMM=X if NAICS2012==X
collapse (mean) EMP_PCT, by(IMM advice)
reshape wide EMP_PCT, i(advice) j(IMM)
gen ratio0=EMP_PCT72/EMP_PCT0
format EMP_PCT* %3.1f
format ratio* %3.2f
gsort -ratio0
order advice EMP_PCT72 EMP_PCT23 EMP_PCT0 ratio0
list, clean noobs

*********************************
*** MSA-LEVEL ANALYSIS 2000   ***
*********************************

use ethn isox2000 self if self==1 using ./temp/2000-Core1, clear
keep ethn isox2000
duplicates drop
sort ethn
save ./temp/2000-iso, replace
use ./temp/2000-MSA-Overage-SelfEmp, clear
sort ethn
merge ethn using ./temp/2000-iso, nok
drop _m
sort ethn
merge ethn using ./temp/iv-controls, nok
drop _m
drop if OVER1==. | ctself==0 | ctself==. | iso==. | ethn==1 | metaread==0
ren OVER1 OVER1t
gen trial=ctselfobs
sort trial
merge trial using ./data-input/trials2, nok
ren OVER1 ROVER1
ren OVER1t OVER1
drop _m
gen wt=ln(cttot)
for var cttot: replace X=-ln(X)

foreach y of var OVER* ct* iso* ROVER1 {
	qui su `y', d
	replace `y'=r(p90) if `y'>r(p90) & `y'!=.
	replace `y'=r(p10) if `y'<r(p10)
	qui su `y', d
	replace `y'=(`y'-r(mean))/r(sd)
	}

*** MSA regressions post winsorize
xi i.ethn 
eststo clear
estimates clear
eststo: areg OVER1 cttot isox [aw=wt], a(metaread) cl(ethn)
eststo: areg OVER1 cttot isox ROVER1 [aw=wt], a(metaread) cl(ethn)
eststo: qui areg OVER1 cttot isox ROVER1 _Ie* [aw=wt], a(metaread) cl(ethn)
eststo: areg OVER1 cttot isox C* [aw=wt], a(metaread) cl(ethn)
eststo: areg OVER1 cttot isox ROVER1 C* [aw=wt], a(metaread) cl(ethn)
eststo: qui areg OVER1 cttot isox ROVER1 _Ie* C* [aw=wt], a(metaread) cl(ethn)
esttab _all using ".\output\T10.csv", replace se r2 ar2 b(%9.3f) se(%9.3f) star( + .1 ++ .05 +++ .01) order(cttot isox) keep(cttot isox)

erase ./temp/2000-iso.dta

*********************************
*** PANEL 1980 - 2018         ***
*********************************

* Ethnic controls
use if ethn!=1 using ./temp/1a-US-Master2.dta, clear
g valid=(sex==1 & approx_age>=16 & age>=22 & age<=70 & ethn!=1 & classwkr!=0)
keep if valid==1
gen Ztotal=perwt
gen Cage2235=perwt if age>=22 & age<=35
gen Cage3655=perwt if age>=36 & age<=55
gen Cage5670=perwt if age>=56 & age<=70
gen Cmarried=perwt if marst==1 | marst==2
gen CEngWell=perwt if speakeng>=2 & speakeng<=5
gen CHS=perwt if educ<=6
gen CSC=perwt if educ>=7 & educ<=9
gen CCol=perwt if educ>=10 & educ!=.
collapse (sum) C* Ztotal, by(ethn year) fast
for var C*: replace X=X/Ztotal
drop Ztotal
sort ethn year
save ./temp/iv-controls-panel, replace

* Simulations based upon group count (no perwt)
use ./temp/1980-WorkerResults.dta, clear
gen year=1980
for num 1990 2000 2010 2018: append using ./temp/X-WorkerResults.dta \ replace year=X if year==.
gen trial=1 if classwkr==1
collapse (sum) trial, by(ethn year) fast
drop if ethn==1
replace trial=10055 if trial>10055 & trial!=.
sort trial
merge trial using ./data-input/trials2, nok
drop _m OVER1sd trial
renpfix O QO
sort ethn year
save ./temp/temp-sim4, replace

use ./temp/1980-Core1, clear 
for num 1990 2000 2010 2018: append using ./temp/X-Core1
keep if self==1
ren census year
gen isox=.
for num 1980 1990 2000 2010 2018: replace isox=isoxX if year==X
gen size=.
for num 1980 1990 2000 2010 2018: replace size=sizeX if year==X
keep ethn year OVER1 OVER2 OVER3 OVER4 size isox weight
sort ethn year
merge ethn year using ./temp/iv-controls-panel, nok
drop _m
sort ethn year
merge ethn year using ./temp/temp-sim4, nok
drop _m
sum

*** Keep full panel
drop if OVER1==. | size==. | isox==. | weight==.
egen temp1=count(year), by(ethn)
tab ethn if temp1<5
drop if temp1<5
drop temp1

*** Variable prep
gen temp1=weight if year==1980
egen wt80=mean(temp1), by(ethn) 
ren weight wt1
drop temp*
for var size: replace X=-X
foreach y of var OVER1 QOVER1 OVER2 OVER3 OVER4 size isox {
	egen temp1=pctile(`y'), p(01) by(year)
	replace `y'=temp1 if `y'<temp1
	drop temp1
	egen temp1=pctile(`y'), p(99) by(year)
	replace `y'=temp1 if `y'>temp1 & `y'!=. 
	drop temp1
	qui su `y', d
	replace `y'=(`y'-r(mean))/r(sd)
	}
for var OVER1: gen temp0=X if year==1980 \ egen X_80=max(temp0), by(ethn) \ gen X_80tr=X_80*year  \ drop temp* 
xi i.year

*** FE regressions 1980 - 2018
eststo clear
estimates clear
eststo: areg OVER1 size isox _I* [aw=wt1], a(ethn) cl(ethn)
eststo: areg OVER1 size isox QOVER1 _I* [aw=wt1], a(ethn) cl(ethn)
eststo: areg OVER1 size isox OVER1_80tr _I* [aw=wt1], a(ethn) cl(ethn)
eststo: areg OVER1 size isox Cage3655-CCol _I* [aw=wt1], a(ethn) cl(ethn)
eststo: areg OVER1 size isox QOVER1 Cage3655-CCol _I* [aw=wt1], a(ethn) cl(ethn)
eststo: areg OVER1 size isox OVER1_80tr Cage3655-CCol _I* [aw=wt1], a(ethn) cl(ethn)
esttab _all using ".\output\T4.csv", replace se r2 ar2 b(%9.3f) se(%9.3f) star( + .1 ++ .05 +++ .01) order(size isox) keep(size isox)

erase ./temp/temp-sim4.dta
erase ./temp/iv-controls-panel.dta

* End of program
log close